package com.axx.android.profile.tool;


import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.axx.android.R;
import com.axx.android.letter.model.PayEvent;
import com.axx.android.letter.model.Tool;
import com.axx.android.utils.DateUtils;
import com.axx.android.utils.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class ToolBuyFragment extends DialogFragment {


    @Bind(R.id.iv_image)
    ImageView ivImage;
    @Bind(R.id.tv_tool_name)
    TextView tvToolName;
    @Bind(R.id.tv_tool_speed)
    TextView tvToolSpeed;
    @Bind(R.id.tv_tool_desc)
    TextView tvToolDesc;
    @Bind(R.id.tv_tool_add)
    TextView tvToolAdd;
    @Bind(R.id.tv_tool_count)
    TextView tvToolCount;
    @Bind(R.id.tv_tool_remove)
    TextView tvToolRemove;
    @Bind(R.id.view_wx_icon)
    View viewWxIcon;
    @Bind(R.id.layout_wx)
    RelativeLayout layoutWx;
    @Bind(R.id.view_ali_icon)
    View viewAliIcon;
    @Bind(R.id.layout_ali)
    RelativeLayout layoutAli;
    @Bind(R.id.tv_pay_price)
    TextView tvPayPrice;
    @Bind(R.id.layout_pay)
    LinearLayout layoutPay;
    @Bind(R.id.layout_pay_style)
    LinearLayout layoutPayStyle;
    @Bind(R.id.tv_pay_price_prefix)
    TextView tvPayPricePrefix;
    @Bind(R.id.tv_energy)
    TextView tvEnergy;
    @Bind(R.id.tv_tool_time)
    TextView tvToolTime;
    private boolean isSingle;

    private View curSelect;

    private Tool.ToollistBean.ToolsBean toolsBean;
    private int payStyle = 1;
    private boolean isFree = false;
    private double distance;

    public ToolBuyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tool_buy, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        ImageLoader.loadImage(toolsBean.getPic(), ivImage, getActivity());
        tvToolSpeed.setText("速度:" + toolsBean.getSpeed() + "km/h");
        tvToolDesc.setText(toolsBean.getIntroduce());
        tvToolName.setText(toolsBean.getName());
        tvEnergy.setText("邮票:" + toolsBean.getEnergy()+"张");
        if (isSingle) {
            tvToolAdd.setVisibility(View.GONE);
            tvToolRemove.setVisibility(View.GONE);
            tvToolCount.setVisibility(View.GONE);
            tvToolTime.setText("耗时:" + getTime() + "h");

            if (toolsBean.getNum() != 0) {
                layoutPayStyle.setVisibility(View.GONE);
                tvPayPricePrefix.setVisibility(View.GONE);
                tvPayPrice.setText("发送信件");
                tvPayPrice.setTextColor(Color.WHITE);
                isFree = true;
            } else {
                curSelect = viewWxIcon;
                viewWxIcon.setSelected(true);
                tvPayPrice.setText(toolsBean.getPrice() + "");
            }
        } else {
            curSelect = viewWxIcon;
            viewWxIcon.setSelected(true);
            tvPayPrice.setText(toolsBean.getPrice() + "");
        }

    }

    private String getTime() {
        double time = distance / toolsBean.getSpeed();
        String stringTime = String.format(Locale.US, "%.2f", time);
        return stringTime.equals("0.00") ? "0.01" : stringTime;
    }

    public static ToolBuyFragment newInstance(Tool.ToollistBean.ToolsBean toolsBean, boolean isSingle) {

        Bundle args = new Bundle();
        args.putBoolean("isSingle", isSingle);
        args.putSerializable("model", toolsBean);

        ToolBuyFragment fragment = new ToolBuyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static ToolBuyFragment newInstance(Tool.ToollistBean.ToolsBean toolsBean, boolean isSingle, double distance) {

        Bundle args = new Bundle();
        args.putBoolean("isSingle", isSingle);
        args.putSerializable("model", toolsBean);
        args.putDouble("distance", distance);

        ToolBuyFragment fragment = new ToolBuyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isSingle = getArguments().getBoolean("isSingle");
        toolsBean = (Tool.ToollistBean.ToolsBean) getArguments().getSerializable("model");
        distance = getArguments().getDouble("distance");
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.9), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.layout_wx, R.id.layout_ali, R.id.tv_tool_add, R.id.tv_tool_remove, R.id.layout_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_wx:
                if (curSelect != null) {
                    curSelect.setSelected(false);
                }
                payStyle = 1;
                curSelect = viewWxIcon;
                curSelect.setSelected(true);
                break;
            case R.id.layout_ali:
                if (curSelect != null) {
                    curSelect.setSelected(false);
                }
                payStyle = 2;
                curSelect = viewAliIcon;
                curSelect.setSelected(true);
                break;
            case R.id.tv_tool_add:
                addCount();
                break;
            case R.id.tv_tool_remove:
                removeCount();
                break;
            case R.id.layout_pay:
                PayEvent event = new PayEvent();
                event.isFree = isFree;
                if (!isFree) {
                    event.payStyle = payStyle;
                    event.buyNum = Integer.valueOf(tvToolCount.getText().toString());
                    event.money = Double.parseDouble(tvPayPrice.getText().toString());
                }
                EventBus.getDefault().post(event);
                break;

        }
    }

    private void addCount() {
        int count = Integer.valueOf(tvToolCount.getText().toString());
        count++;
        double price = toolsBean.getPrice() * count;
        tvPayPrice.setText(String.format("%.2f", price));
        tvToolCount.setText(count + "");
    }

    private void removeCount() {
        int count = Integer.valueOf(tvToolCount.getText().toString());
        if (count - 1 >= 1) {
            count--;
            double price = toolsBean.getPrice() * count;
            tvToolCount.setText(count + "");
            tvPayPrice.setText(String.format("%.2f", price));
        }
    }
}
